package com.andova.file

import android.content.Context
import androidx.test.core.app.ActivityScenario
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.andova.MainActivity
import com.andova.update.ApkUpdater
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import org.junit.Test
import org.junit.runner.RunWith
import java.io.File
import java.text.SimpleDateFormat

@RunWith(AndroidJUnit4::class)
class FileRecoveryTest {
    @Test
    fun testRecover() {
        val fr = FileRecovery()
        val context = ApplicationProvider.getApplicationContext<Context>()
        val file = File("/storage/emulated/0/Android/data/cn.com.yzwl.scDOOR.androidphone/files/face")
        GlobalScope.launch {
            fr.recover(file, SimpleDateFormat("yyyy-MM-dd")) { return@recover false }
        }
        Thread.sleep(30000L)
    }

    @Test
    fun testApkUpdater() {
        ActivityScenario.launch(MainActivity::class.java).onActivity {
            ApkUpdater(it, ApkUpdater.Builder(
//                "https://www.mediafire.com/file/vaj7n2vo682diyy/com.tencent.mobileqq_8.4.1-1442_rawapk.com.apk",
                "http://a.xzfile.com/android/wxmnyyy_downcc.apk",
                "Test apk",
                true,
                true,
                false,
                object : ApkUpdater.ApkUpdateListener {
                    override fun onCancel() = println("onCancel")
                    override fun onReady(direct: Boolean) = println("onReady:$direct")
                    override fun onError(throwable: Throwable?) = println("onError:$throwable")
                    override fun onFailure(apkUpdater: ApkUpdater?) = println("onFailure")
                    override fun onSuccess(apkUpdater: ApkUpdater?, path: String?) {
                        println("onSuccess:$path")
                        apkUpdater?.dismiss()
                    }
                }
            )).asyncUpdate("1.0.1")
        }
        while (true) {
            Thread.sleep(1000L)
        }
    }
}